package com.adinnet.api.appInform.controller;


import com.adinnet.api.appInform.model.AppInform;
import com.adinnet.api.appInform.model.AppInformIdReq;
import com.adinnet.api.appInform.model.AppInformReq;
import com.adinnet.api.appInform.model.form.AppInformForm;
import com.adinnet.api.appInform.service.AppInformService;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResp;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * app站内、极光通知 前端控制器
 * </p>
 *
 * @author liukp
 * @since 2020-09-03
 */
@RestController
@RequestMapping("api/appInform")
public class AppInformController extends BaseController {

    @Autowired
    private AppInformService appInformService;

    /**
     * 查询消息列表
     * @return
     */
    @PostMapping("getAppInformList")
    public PageResp getAppInform(@RequestBody AppInformForm req) {
        try {
            if(req.getChildId()==null){
                return new PageResp("查询失败,儿童id不能为空");
            }
            return appInformService.queryList(req);
        }catch (Exception e){
            return new PageResp("查询失败");
        }
    }

    /**
     * 查询消息详情
     * @return
     */
    @GetMapping("details")
    public JsonResult details(@RequestParam(value = "informId") Integer informId) {
        try {
            if(informId==null){
                return JsonResult.error("消息id不能为空");
            }
            AppInform details = appInformService.getById(informId);
            return JsonResult.ok().put("data",details);
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }

    /**
     * @description 标记消息已读
     * @author lkp
     * @param
     * @param: idList
     * @createTime 2020/12/11 10:58
     */
    @PostMapping("updateRead")
    public JsonResult updateRead(@Valid @RequestBody AppInformIdReq req) {
        try {
            List<AppInform> idList = req.getIdList();
            if(idList==null || idList.size()<1){
                return JsonResult.error("消息id集合不能为空");
            }
            appInformService.updateRead(idList);
            return JsonResult.ok();
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }
}
